﻿using Lhz.Data;
using Lhz.Framework.Test.Entity;
using Lhz.InternetService;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Linq;

namespace Lhz.Framework.Test.LhzInternetService
{
    [TestClass]
    public class UnitTestServiceInvoker
    {
        [TestMethod]
        public void ServiceInvokerTestMethodWebServiceWithSession()
        {
            var url = "http://localhost:53843/WebServiceCookieTest.asmx";
            var wsi = new ServiceInvoker() { AllowCookie = true };
            var assertId = 1;
            for (int i = 0; i < 3; i++)
            {
                var id = wsi.Invoke(url, "GetId");
                Assert.AreEqual(assertId, id);
                assertId++;
            }
            wsi.AllowCookie = false;
            for (int i = 0; i < 3; i++)
            {
                var id = wsi.Invoke(url, "GetId");
                Assert.AreEqual(1, id);
            }
            wsi.AllowCookie = true;
            for (int i = 0; i < 3; i++)
            {
                var id = wsi.Invoke(url, "GetId");
                Assert.AreEqual(assertId, id);
                assertId++;
            }
        }

        [TestMethod]
        public void ServiceInvokerTestMethodGetMethodParameterTypes()
        {
            var url = "http://localhost:53843/ServiceStudent.svc";
            var si = new ServiceInvoker(typeof(OrderBy), typeof(Student));
            var types = si.GetMethodParameterTypes(url, "Query");
            Assert.AreEqual(1, types.Length);
            types = si.GetMethodParameterTypes(url, "QueryByOrderBies");
            Assert.AreEqual(2, types.Length);
            Assert.AreEqual(typeof(OrderBy[]), types.First());
            types = si.GetMethodParameterTypes(url, "QueryByPk");
            Assert.AreEqual(2, types.Length);
            Assert.AreEqual(typeof(Guid), types.First());
            types = si.GetMethodParameterTypes(url, "Add");
            Assert.AreEqual(1, types.Length);
            Assert.AreEqual(typeof(Student), types.First());
            types = si.GetMethodParameterTypes(url, "Adds");
            Assert.AreEqual(1, types.Length);
            Assert.AreEqual(typeof(Student[]), types.First());
            types = si.GetMethodParameterTypes(url, "DeleteByPks");
            Assert.AreEqual(1, types.Length);
            Assert.AreEqual(typeof(Guid[]), types.First());
        }

        [TestMethod]
        public void ServiceInvokerTestMethodInvokeWcfServiceWithStringArrayArgument()
        {
            var url = "http://localhost:53843/ServiceTest.svc";
            var argument = new string[] { "a", "b", "c", "d" };
            var si = new ServiceInvoker();
            var result = si.Invoke(url, "GetStringArrayLength", new object[] { argument });
            Assert.AreEqual(argument.Length, result);
        }

        [TestMethod]
        public void ServiceInvokerTestMethodInvokeWcfServiceWithStudentArrayArgument()
        {
            var url = "http://localhost:53843/ServiceTest.svc";
            var argument = new Student[] { new Student(), new Student(), new Student() };
            var si = new ServiceInvoker();
            var result = si.Invoke(url, "GetStudentArrayLength", new object[] { argument });
            Assert.AreEqual(argument.Length, result);
        }

        [TestMethod]
        public void ServiceInvokerTestMethodInvokeWcfServiceQueryStudents()
        {
            var url = "http://localhost:53843/ServiceTest.svc";
            var si = new ServiceInvoker(typeof(Student));
            var students = si.Invoke<Student[]>(url, "QueryStudents");
        }

        [TestMethod]
        public void ServiceInvokerTestMethodInvokeWebServiceWithStringArrayArgument()
        {
            var url = "http://localhost:53843/WebServiceTest.asmx";
            var argument = new string[] { "a", "b", "c", "d" };
            var si = new ServiceInvoker();
            var result = si.Invoke(url, "GetStringArrayLength", new object[] { argument });
            Assert.AreEqual(argument.Length, result);
        }

        [TestMethod]
        public void ServiceInvokerTestMethodInvokeWebServiceWithStudentArrayArgument()
        {
            var url = "http://localhost:53843/WebServiceTest.asmx";
            var argument = new Book[] { new Book(), new Book(), new Book() };
            var si = new ServiceInvoker();
            var result = si.Invoke(url, "GetBookArrayLength", new object[] { argument });
            Assert.AreEqual(argument.Length, result);
        }
    }
}
